package com.example.carrierapp.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.carrierapp.event.BoroughsBean;
import com.example.carrierapp.event.CitiesBean;
import com.example.carrierapp.event.ProvinceBean;
import com.example.lb.fingerdemo.R;

import java.util.List;

/**
 * C城市选择器适配器
 */
public class CityChooseAdapter extends RecyclerView.Adapter {

    private List<ProvinceBean> mData;
    private List<CitiesBean> mCitiesList;
    private List<BoroughsBean> mBoroughsList;

    //判断当前界面（省/市/区）
    private int addressType;

    private int mCityPosition;

    public static final int TYPE_PROVINCE = 0;
    public static final int TYPE_CITY = 1;
    public static final int TYPE_BOROUGH = 2;

    public String mCurrentCity;
    public int mCurrentId;

    public int getAddressType() {
        return addressType;
    }

    public void setAddressType(int addressType) {
        this.addressType = addressType;
    }

    private Context mContext;

    public CityChooseAdapter(Context context, List<ProvinceBean> data) {
        mData = data;
        mContext = context;
    }

    //定义一个监听对象，用来储存监听事件
    public OnCityItemClickListener mOnCityItemClickListener;

    public void setOnCityChooseItemListener(OnCityItemClickListener listener) {
        mOnCityItemClickListener = listener;
    }

    public interface OnCityItemClickListener {

        void onItemClick(View view, int position);
    }

    public class CityChooseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView mItemView;
        public LinearLayout mLinearLayout;

        //初始化ViewHolder，此处绑定后在onBindViewHolder中可以直接使用
        public CityChooseViewHolder(View itemView) {
            super(itemView);
            mItemView = (TextView) itemView.findViewById(R.id.tv_city_item);
            mLinearLayout = (LinearLayout) itemView;
            mLinearLayout.setOnClickListener(this);
        }


        //通过接口回调来实现Recycleview的点击事件
        @Override
        public void onClick(View view) {
            if (mOnCityItemClickListener != null) {
                mOnCityItemClickListener.onItemClick(view, getPosition());
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CityChooseViewHolder viewHolder = new CityChooseViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_city, parent, false));

        return viewHolder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        CityChooseViewHolder viewHolder = (CityChooseViewHolder) holder;

        switch (getAddressType()) {
            case TYPE_PROVINCE:
                viewHolder.mItemView.setText(mData.get(position).getProvinceName());
                break;
            case TYPE_CITY:
                viewHolder.mItemView.setText(mCitiesList.get(position).getCityName());
                mCityPosition = position;
                break;
            case TYPE_BOROUGH:
                viewHolder.mItemView.setText(mBoroughsList.get(position).getBoroughName());
//                    mCurrentCity = mBoroughsList.get(position).getBoroughName();
//                    mCurrentId = mBoroughsList.get(position).getId();

                break;
        }


//        viewHolder.mItemView.setTextColor(mContext.getResources().getColor(R.color.oringe_light));
    }

    public void setCityList(int position) {

        mCitiesList = mData.get(position).getCities();
    }

    public void setBoroughList(int provincePosition, int position) {
        if (mCitiesList == null) {
            mCitiesList = mData.get(provincePosition).getCities();
        }
        mBoroughsList = mCitiesList.get(position).getBoroughs();
    }

    public void setProvincePosition(int position) {
        if (position == 0) {
            mCurrentCity = mData.get(position).getProvinceName();
            mCurrentId = mData.get(position).getId();
        }
    }

    public void setCityposition(int position) {
        mCurrentCity = mCitiesList.get(position).getCityName();
        mCurrentId = mCitiesList.get(position).getId();
    }

    //获取当前市区信息，当点击区级列表中全部时返回市区信息
    public void setBoroughsposition(int cityPosition, int position) {
        if (position != 0) {
            mCurrentCity = mBoroughsList.get(position).getBoroughName();
            mCurrentId = mBoroughsList.get(position).getId();
        } else {
            setCityposition(cityPosition);
        }
    }

    @Override
    public int getItemCount() {
        switch (addressType) {
            case TYPE_PROVINCE:
                return mData.size();
            case TYPE_CITY:
                return mCitiesList.size();
            case TYPE_BOROUGH:
                return mBoroughsList.size();
        }
        return 0;

    }

}
